using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class LoadManager_Level : MonoBehaviour
{
    public GameObject loadScreen;
    public Slider loadSlider;
    public Text text;

    public static LoadManager_Level instance;

    private void Awake()
    {
        instance = this;
    }

    public void LoadEvent()
    {
        loadScreen.SetActive(true);
        Invoke("LoadNextLevel", 2f);
    }

    public void LoadNextLevel()
    {

        StartCoroutine(LoadLevel());
        Time.timeScale = 0;
    }

    IEnumerator LoadLevel()
    {


        AsyncOperation operation = SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex + 1);

        operation.allowSceneActivation = false;

        while (!operation.isDone)
        {
            loadSlider.value = operation.progress;

            text.text = operation.progress * 100 + "%";

            if (operation.progress >= 0.9f)
            {
                loadSlider.value = 1;

                text.text = "Press AnyKey to Continue";
                if (Input.anyKeyDown)
                {
                    Time.timeScale = 1;
                    operation.allowSceneActivation = true;
                }
            }
            yield return null;
        }
    }
}
